package com.mall.common.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mall.b2c.util.JsonDataGridHelper;
import com.mall.common.bean.Pager;
import com.mall.common.domain.component.PaymentTypeDomain;
import com.mall.common.enums.PayType;
import com.mall.common.service.PaymentTypeService;
import com.mall.common.util.EnumUtils;
import com.mall.common.util.ResponseUtils;
import com.mall.common.validation.annotation.RequiredFieldValidator;
import com.mall.common.validation.annotation.RequiredStringValidator;
import com.mall.common.validation.annotation.Validations;

/**
 * 后台Action类 - 支付方式
 */
@Controller
@RequestMapping("/admin/system/paymentType")
public class PaymentTypeController extends BaseController{

	@Resource private PaymentTypeService paymentTypeService;
	
	/**
	 * 跳转到列表页面
	 * @return
	 */
	@RequestMapping(value="/list", method=RequestMethod.GET)
	public String list(Model model){
		return "/admin/system/paymenttype/list";
	}
	
	//获取列表
	@RequestMapping(value="/queryData", method=RequestMethod.POST)
	@ResponseBody
	public Pager queryData(Pager pager){
		pager = paymentTypeService.findPage(pager);
		return JsonDataGridHelper.createJSONData(pager);
	}
	
	/**
	 * 跳转到新增页面
	 * @return
	 */
	@RequestMapping(value="/add", method=RequestMethod.GET)
	public String input(Model model){
		model.addAttribute("payTypes", EnumUtils.enumProp2I18nList(PayType.class));
		return "/admin/system/paymenttype/input";
	}
	
	/**
	 * 跳转到修改页面
	 * @return
	 */
	@Validations(
		requiredFields = {
				@RequiredFieldValidator(fieldName = "id", message = "支付方式不存在!")
		}
	)
	@RequestMapping(value="/edit", method=RequestMethod.GET)
	public String edit(Long id, Model model){
		model.addAttribute("paymentType", paymentTypeService.find(id));
		model.addAttribute("payTypes", EnumUtils.enumProp2I18nList(PayType.class));
		return "/admin/system/paymenttype/input";
	}
	
	/**
	 * 校验支付方式名称
	 * @return
	 */
	@RequestMapping(value="/checkName")
	@ResponseBody
	public String checkName(PaymentTypeDomain paymentType, String oldValue){
		String newValue = paymentType.getName();
		if (paymentTypeService.isUnique("name", oldValue, newValue)) {
			return "true";
		} else {
			return "false";
		}
    }
    
    /**
	 * 保存
	 * @return
	 */
    @Validations(
    		requiredFields = {
    				@RequiredFieldValidator(fieldName = "payType", message = "支付类型不存在!"),
    				@RequiredFieldValidator(fieldName = "component.id", message = "关联不存在!")
    		},
			requiredStrings = {
					@RequiredStringValidator(fieldName = "name", message = "支付方式名称不允许为空!")
			}
	)
	@RequestMapping(value="/save",method=RequestMethod.POST)
	public String save(PaymentTypeDomain paymentType, HttpServletResponse response){
    	paymentTypeService.save(paymentType);
    	return ResponseUtils.renderJSON(successMessage, response);
    }
    
    /**
	 * 修改
	 * @return
	 */
    @Validations(
    		requiredFields = {
    				@RequiredFieldValidator(fieldName = "id", message = "支付方式不存在!"),
    				@RequiredFieldValidator(fieldName = "payType", message = "支付类型不存在!"),
    				@RequiredFieldValidator(fieldName = "component.id", message = "关联不存在!")
    		},
			requiredStrings = {
					@RequiredStringValidator(fieldName = "name", message = "支付方式名称不允许为空!")
			}
	)
	@RequestMapping(value="/update",method=RequestMethod.POST)
	public String update(PaymentTypeDomain paymentType, HttpServletResponse response){
    	paymentTypeService.save(paymentType);
    	return ResponseUtils.renderJSON(successMessage, response);
    }
    
    /**
	 * 删除支付方式
	 * @return
	 */
    @Validations(
		requiredFields = {
				@RequiredFieldValidator(fieldName = "ids", message = "ids不允许为空!")
		}
	)
	@RequestMapping(value="/delete",method=RequestMethod.POST)
	public String delete(Long[] ids, HttpServletResponse response){
    	paymentTypeService.remove(ids);
    	return ResponseUtils.renderJSON(successMessage, response);
    }

}
